package com.server.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.server.model.all.enums.YesNoEnum;
import com.server.common.code.utils.BeanUtils;
import com.server.model.all.util.TreeUtils;
import com.server.model.all.user.dto.ListMenuDto;
import com.server.model.all.user.dto.TreeMenuDto;
import com.server.model.all.user.entity.MenuEntity;
import com.server.model.all.user.entity.UserEntity;
import com.server.user.mapper.MenuMapper;
import com.server.user.service.UserService;
import com.server.model.all.user.vo.TreeMenuVo;
import com.server.user.service.MenuService;
import com.server.model.all.user.vo.ListMenuVo;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, MenuEntity> implements MenuService {

    @Resource
    private MenuMapper menuMapper;

    @Resource
    private UserService userService;

    @Override
    public List<ListMenuVo> listMenu(ListMenuDto listMenuDto) {

        List<MenuEntity> menuEntityList = list(new LambdaQueryWrapper<MenuEntity>()
                .eq(MenuEntity::getDelFlag, YesNoEnum.NO.getCode())
        );

        List<ListMenuVo> listMenuVoList = BeanUtil.copyToList(menuEntityList, ListMenuVo.class);

        //创建的用户ID集合
        List<Long> createUserIdList = listMenuVoList.stream().map(ListMenuVo::getCreateUserId).collect(Collectors.toList());
        List<UserEntity> userEntityList = userService.list(new LambdaQueryWrapper<UserEntity>()
                .select(UserEntity::getUserName,UserEntity::getRealName)
                .eq(UserEntity::getUserId, createUserIdList)
        );

        for (ListMenuVo listMenuVo : listMenuVoList) {

            //用户名和真实姓名
            UserEntity userEntityNew = userEntityList.stream()
                    .filter(userEntity -> userEntity.getUserId().equals(listMenuVo.getCreateUserId()))
                    .findFirst().orElse(new UserEntity());

            listMenuVo.setUserName(userEntityNew.getUserName());
            listMenuVo.setRealName(userEntityNew.getRealName());

        }

        return listMenuVoList;
    }

    @Override
    public List<TreeMenuVo> treeMenu(TreeMenuDto treeMenuDto) {
        List<TreeMenuVo> treeMenuVoList = new ArrayList<>();

        List<MenuEntity> menuEntityList = list(new LambdaQueryWrapper<MenuEntity>()
                .eq(MenuEntity::getDelFlag, YesNoEnum.NO.getCode())
                .eq(MenuEntity::getStatus, YesNoEnum.YES.getCode())
        );

        if(ObjectUtils.isEmpty(menuEntityList)){
            return treeMenuVoList;
        }

        for (MenuEntity menuEntity:menuEntityList){
            TreeMenuVo treeMenuVo = BeanUtils.copyProperties(menuEntity,TreeMenuVo.class);
            treeMenuVo.setLabel(menuEntity.getRemarks());
            treeMenuVoList.add(treeMenuVo);
        }

        TreeUtils<TreeMenuVo> treeUtils = new TreeUtils<>();
        return treeUtils.buildTreeSelect(treeMenuVoList);
    }


}
